﻿using Dapper;
using ERPEF.Model;
using ERPIService;
using ERPService;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Hosting;
using System;
using System.Data;

namespace S4Project.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Consumes("application/json")]
    public class dict_dataController : ControllerBase
    {
        private readonly bs_dict_dataService bs_dict_data;
        private readonly IWebHostEnvironment environment;
        private readonly MyDbContext myDbContext;

        
        public dict_dataController(MyDbContext myDb, bs_dict_dataService bs_Dict_DataService, IWebHostEnvironment environment)
        {
            myDbContext = myDb;
            bs_dict_data = bs_Dict_DataService;
            this.environment = environment;
        }

        //客户属性查询
        [HttpGet]
        public async Task<IActionResult> Queryclient()
        {
            var warehouse = await bs_dict_data.Queryclient();
            if (warehouse != null)
            {
                return Ok(warehouse);
            }
            else
            {
                return NotFound("404");
            }
        }

        //物品属性查询
        [HttpGet]
        public async Task<IActionResult> Querygoods()
        {
            var warehouse = await bs_dict_data.Querygoods();
            if (warehouse != null)
            {
                return Ok(warehouse);
            }
            else
            {
                return NotFound("404");
            }
        }

        //添加客户
        [HttpPost]
        public async Task<IActionResult> clientAdd(bs_dict_data DictData)
        {
            if (ModelState.IsValid)
            {   
                int result = await bs_dict_data.clientAdd(DictData);
               
                if (result > 0)
                {
                    return Ok("添加成功！");
                }
                else
                {
                    return BadRequest("添加失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

        //添加物品
        [HttpPost]
        public async Task<IActionResult> goodsAdd(bs_dict_data DictData)
        {
            if (ModelState.IsValid)
            {
                int result = await bs_dict_data.goodsAdd(DictData);

                if (result > 0)
                {
                    return Ok("添加成功！");
                }
                else
                {
                    return BadRequest("添加失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }
        //客户属性修改
        [HttpPut]
        public async Task<IActionResult> clientUpdate(bs_dict_data DictData)
        {
            if (ModelState.IsValid)
            {
                int result = await bs_dict_data.clientUpdate(DictData);
                if (result > 0)
                {
                    return Ok("修改成功！");
                }
                else
                {
                    return BadRequest("修改失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

        //物品属性修改
        [HttpPut]
        public async Task<IActionResult> goodsUpdate(bs_dict_data DictData)
        {
            if (ModelState.IsValid)
            {
                int result = await bs_dict_data.goodsUpdate(DictData);
                if (result > 0)
                {
                    return Ok("修改成功！");
                }
                else
                {
                    return BadRequest("修改失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }
        //客户逻辑删除
        [HttpDelete("{dd_code}")]
        public async Task<IActionResult> clientDelete(int dd_code)
        {
            int result = await bs_dict_data.clientDelete(dd_code);
            if (result > 0)
            {
                return Ok("删除成功！");
            }
            else
            {
                return NotFound("未找到");
            }
        }

        //物品逻辑删除
        [HttpDelete("{dd_code}")]
        public async Task<IActionResult> goodsDelete(int dd_code)
        {
            int result = await bs_dict_data.clientDelete(dd_code);
            if (result > 0)
            {
                return Ok("删除成功！");
            }
            else
            {
                return NotFound("未找到");
            }
        }
        //客户属性分页
        [HttpGet]
        public ActionResult<FenYe<bs_dict_data>> getClientFenYe(int currentPage, int pageSize, string where)
        {
            using (var conn = myDbContext.Database.GetDbConnection())
            {
                conn.Open();
                var parameters = new DynamicParameters();
                parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
                parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "dd_code"); // 更改为正确的列名
                parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "bs_dict_data"); // 更改为正确的表名
                parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
                parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: where); // 添加 where 参数
                parameters.Add("row", dbType: DbType.Int32, direction: ParameterDirection.Output); // 修改为和存储过程中一致的参数名
                string sql = "[dbo].[FenYe]";
                var result = conn.Query<bs_dict_data>(sql, parameters, commandType: CommandType.StoredProcedure);
                int row = parameters.Get<int>("row");
                FenYe<bs_dict_data> fenYe = new FenYe<bs_dict_data>()
                {
                    clientData = result,
                    Rows = row,
                };

                if (fenYe.clientData.Any())
                {
                    return fenYe;
                }
                else
                {
                    return Content("没有查到数据");
                }
            }
        }
        //物品属性分页
        [HttpGet]
        public ActionResult<FenYe<bs_dict_data>> getGoodsFenYe(int currentPage, int pageSize, string where)
        {
            using (var conn = myDbContext.Database.GetDbConnection())
            {
                conn.Open();
                var parameters = new DynamicParameters();
                parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
                parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "dd_code"); // 更改为正确的列名
                parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "bs_dict_data"); // 更改为正确的表名
                parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
                parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: where); // 添加 where 参数
                parameters.Add("row", dbType: DbType.Int32, direction: ParameterDirection.Output); // 修改为和存储过程中一致的参数名
                string sql = "[dbo].[FenYe]";
                var result = conn.Query<bs_dict_data>(sql, parameters, commandType: CommandType.StoredProcedure);
                int row = parameters.Get<int>("row");
                FenYe<bs_dict_data> fenYe = new FenYe<bs_dict_data>()
                {
                    goodsData = result,
                    Rows = row,
                };

                if (fenYe.goodsData.Any())
                {
                    return fenYe;
                }
                else
                {
                    return Content("没有查到数据");
                }
            }
        }

      


        //计量单位查询
        [HttpGet]
        public async Task<IActionResult> Queryunit()
        {
            var warehouse = await bs_dict_data.Queryunit();
            if (warehouse != null)
            {
                return Ok(warehouse);
            }
            else
            {
                return NotFound("404");
            }
        }


        //查询字典--单位
        [HttpGet]
        public async Task<IActionResult> DictQuery()
        {
            var dic = await bs_dict_data.Queryunit();
            var dict = dic.Where(w => w.dd_type == "type_unit").ToList();
            if (dict.Any())
            {
                return Ok(dict);
            }
            else
            {
                return NotFound("404");
            }
        }


        //计量单位分页
        [HttpGet]
        public ActionResult<FenYe<bs_dict_data>> getUnitFenYe(int currentPage, int pageSize, string where)
        {
            using (var conn = myDbContext.Database.GetDbConnection())
            {
                conn.Open();
                var parameters = new DynamicParameters();
                parameters.Add("pageSize", dbType: DbType.Int32, direction: ParameterDirection.Input, value: pageSize);
                parameters.Add("keyName", dbType: DbType.String, direction: ParameterDirection.Input, value: "dd_code"); // 更改为正确的列名
                parameters.Add("tableName", dbType: DbType.String, direction: ParameterDirection.Input, value: "bs_dict_data"); // 更改为正确的表名
                parameters.Add("currentPage", dbType: DbType.Int32, direction: ParameterDirection.Input, value: currentPage);
                parameters.Add("where", dbType: DbType.String, direction: ParameterDirection.Input, value: where); // 添加 where 参数
                parameters.Add("row", dbType: DbType.Int32, direction: ParameterDirection.Output); // 修改为和存储过程中一致的参数名
                string sql = "[dbo].[FenYe]";
                var result = conn.Query<bs_dict_data>(sql, parameters, commandType: CommandType.StoredProcedure);
                int row = parameters.Get<int>("row");
                FenYe<bs_dict_data> fenYe = new FenYe<bs_dict_data>()
                {
                    unitData = result,
                    Rows = row,
                };

                if (fenYe.unitData.Any())
                {
                    return fenYe;
                }
                else
                {
                    return Content("没有查到数据");
                }
            }
        }

        //添加计量单位
        [HttpPost]
        public async Task<IActionResult> unitAdd(bs_dict_data DictData)
        {
            if (ModelState.IsValid)
            {
                int result = await bs_dict_data.unitAdd(DictData);

                if (result > 0)
                {
                    return Ok("添加成功！");
                }
                else
                {
                    return BadRequest("添加失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

        //计量单位修改
        [HttpPut]
        public async Task<IActionResult> unitUpdate(bs_dict_data DictData)
        {
            if (ModelState.IsValid)
            {
                int result = await bs_dict_data.unitUpdate(DictData);
                if (result > 0)
                {
                    return Ok("修改成功！");
                }
                else
                {
                    return BadRequest("修改失败！");
                }
            }
            else
            {
                return BadRequest(ModelState);
            }
        }

        //计量单位逻辑删除
        [HttpDelete("{dd_code}")]
        public async Task<IActionResult> unitDelete(int dd_code)
        {
            int result = await bs_dict_data.unitDelete(dd_code);
            if (result > 0)
            {
                return Ok("删除成功！");
            }
            else
            {
                return NotFound("未找到");
            }
        }


    }

}
